package com.qidian.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.mapper.ProcessMapper;
import com.qidian.mes.pojo.Process;
import com.qidian.mes.service.ProcessService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
   private ProcessMapper processMapper;

    @Override
    public MESResult list(int page, int limit, Process process) {
        PageHelper.startPage(page,limit);
        List<Process> list= processMapper.list(process);
        PageInfo<Process> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }


    @Override
    public MESResult save(Process process) {
        if (processMapper.getByProcessCode(process.getProcessCode()) !=null||
        processMapper.getByProcessName(process.getProcessName()) !=null){
            return MESResult.duplicate("编码或名称重复");
        }
        processMapper.save(process);
        return MESResult.ok();
    }

    @Override
    public MESResult selectById(Long processId) {
        Process process=null;
        if ((process=processMapper.selectByProcessId(processId)) !=null){
            return MESResult.ok(process);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult update(Process process) {
        Process process1 =processMapper.getByProcessCode(process.getProcessCode());
        if (process1 !=null && !process1.getProcessCode().equals(process.getProcessCode())){
            return MESResult.duplicate("编码重复");
        }
        Process process2=processMapper.getByProcessName(process.getProcessName());
        if (process2 !=null && !process2.getProcessName().equals(process.getProcessName())){
            return MESResult.duplicate("名称重复");
        }
        processMapper.update(process);
        return MESResult.ok();
    }

    @Override
    public MESResult delete(Long processId) {
        processMapper.deleteByProcessId(processId);
        return MESResult.ok();
    }
}
